package com.wesley.client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

	private Button btn_shutdown, btn_restart, btn_cancel, btn_close;
	private Socket client_socket;
	private DataOutputStream data_output = null;
	private DataInputStream data_input = null;
	private String connIP = "";
	private ConnThread connThread = null;
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Toast.makeText(MainActivity.this, (String) msg.obj, Toast.LENGTH_SHORT).show();
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newmain);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btnShutdown:
			final String shutdown = "shutdown";
			if (connThread != null) {
				connThread.interrupt();
			}
			connThread = new ConnThread(connIP, 30000, shutdown);
			connThread.start();
			break;
		case R.id.btnReboot:
			final String restart = "restart";
			if (connThread != null) {
				connThread.interrupt();
			}
			connThread = new ConnThread(connIP, 30000, restart);
			connThread.start();
			break;
		case R.id.btnCancel:
			final String cancel = "cancel";
			if (connThread != null) {
				connThread.interrupt();
			}
			connThread = new ConnThread(connIP, 30000, cancel);
			connThread.start();
			break;
		case R.id.btnClose:
			finish();
			break;
		default:
			break;
		}
	}

	class ConnThread extends Thread {
		private String ip;
		private int port;
		private String content;

		public ConnThread(String ip, int port, String content) {
			this.ip = ip;
			this.port = port;
			this.content = content;
		}

		@Override
		public void run() {
			try {
				client_socket = new Socket(ip, port);
				while (true) {
					data_output = new DataOutputStream(client_socket.getOutputStream());
					data_input = new DataInputStream(client_socket.getInputStream());
					String msg = "";
					if ((data_output != null) && (!content.equals(""))) {
						data_output.writeUTF(content);
					}
					msg = data_input.readUTF();
					System.out.println(msg);
					if (msg != null && !"".equals(msg)) {
						Message message = new Message();
						message.what = 1;
						message.obj = msg;
						handler.sendMessage(message);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					if (data_output != null) {
						data_output.close();
					}
					if (data_input != null) {
						data_input.close();
					}
					if (client_socket != null) {
						client_socket = null;
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			super.run();
		}
	}
}
